<template>
  <view class="mask">
    <view class="header">
      本次训练结果
      <view class="close-btn" @click="close"> <uni-icons type="closeempty" size="20"></uni-icons></view>
    </view>
    <view class="status">
      <view class="answer-count">
        <text class="sub-title">错题数：</text>
        <text class="sub-title">{{ errNum }}</text>
        <text class="sub-title">未做题数：</text>
        <text class="sub-title">{{ noDoneNum }}</text>
      </view>
    </view>
    <view class="main">
      <view class="title"> 一、单选题（本大题共{{ singleList.length }}小题） </view>
      <view class="circular-box">
        <view
          class="circular white"
          v-for="(item, index) in singleList"
          :class="{ green: isCorrect(item), red: isError(item) }"
          :key="item.id"
          @click="link(item.navIndex)"
        >
          {{ index + 1 }}
        </view>
      </view>
      <view class="title">二、多选题（本大题共{{ multipleList.length }}小题） </view>
      <view class="circular-box">
        <view
          class="circular white"
          v-for="(item, index) in multipleList"
          :class="{ green: isCorrect(item), red: isError(item) }"
          :key="item.id"
          @click="link(item.navIndex)"
        >
          {{ (singleList.length || 0) + index + 1 }}
        </view>
      </view>
      <view class="title">三、判断题（本大题共{{ judgementList.length }}小题） </view>
      <view class="circular-box">
        <view
          class="circular white"
          v-for="(item, index) in judgementList"
          :class="{ green: isCorrect(item), red: isError(item) }"
          :key="item.id"
          @click="link(item.navIndex)"
        >
          {{ (singleList.length || 0) + (multipleList.length || 0) + index + 1 }}
        </view>
      </view>
    </view>
    <view class="footer">
      <view class="submit" @click="handleEnded">结束训练</view>
    </view>
  </view>
</template>

<script>
import { mapMutations, mapState, mapActions } from 'vuex'
import { get } from 'lodash'
import TipDialog from './components/TipDialog'
import ExaminationDialog from './components/ExaminationDialog'

export default {
  name: 'answer-sheet',
  components: {
    TipDialog,
    ExaminationDialog
  },
  data() {
    return {
      paperData: {
        judgementList: [],
        multipleList: [],
        singleList: []
      },
      typeId: '' // examinationTypeId
    }
  },
  onLoad({ id }) {
    this.typeId = id
  },

  onShow() {
    this.fetchData()
  },

  computed: {
    ...mapState({
      examDetailList: (state) => state.onlineExam.examDetailList
    }),
    isView() {
      return this.mode === 'view'
    },
    singleList() {
      return get(this.paperData, 'singleList', []).map((item, index) => ({
        ...item,
        navIndex: index
      }))
    },
    multipleList() {
      return get(this.paperData, 'multipleList', []).map((item, index) => ({
        ...item,
        navIndex: index + this.singleList.length
      }))
    },
    noDoneNum() {
      return this.examDetailList.filter((item) => !item.selectOptionIds.length).length
    },
    errNum() {
      return this.examDetailList.filter(
        (item) => !item.selectOptionIds.every((ele) => item.correctOptionIds.includes(ele))
      ).length
    },
    judgementList() {
      return get(this.paperData, 'judgementList', []).map((item, index) => ({
        ...item,
        navIndex: index + this.singleList.length + this.multipleList.length
      }))
    }
  },
  methods: {
    ...mapActions(['submitPapel']),
    ...mapMutations(['setCurrentPaperIndex']),
    async fetchData() {
      let examList = this.examDetailList
      for (let e of examList) {
        if (e.type === '1') {
          this.paperData.singleList.push(e)
        } else if (e.type === '2') {
          this.paperData.multipleList.push(e)
        } else {
          this.paperData.judgementList.push(e)
        }
      }
      console.log(this.paperData)
    },
    close() {
      uni.navigateBack()
    },
    isCorrect(item) {
      if (item.selectOptionIds.length !== item.correctOptionIds.length) {
        return false
      }
      return item.selectOptionIds.every((ele) => item.correctOptionIds.includes(ele))
    },
    isError(item) {
      return !this.isCorrect(item) && item.selectOptionIds.length > 0
    },
    link(index) {
      this.setCurrentPaperIndex(index)
      this.close()
    },
    handleEnded() {
      uni.navigateBack({
        delta: 2
      })
    }
  }
}
</script>

<style scoped lang="scss" type="text/scss">
.mask {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  background-color: #fff;
  padding-top: 50rpx;
}
.header {
  position: relative;
  height: 88rpx;
  line-height: 88rpx;
  width: 100%;
  font-size: 36rpx;
  font-weight: bold;
  color: #333333;
  text-align: center;

  .close-btn {
    position: absolute;
    top: 0;
    left: 30rpx;
  }
}
.status {
  display: flex;
  align-items: center;
  font-size: 26rpx;
  height: 77rpx;
  padding-left: 30rpx;
  background: #f5f5f5;

  .title {
    color: #333333;
    margin-right: 10rpx;
  }
  .sub-title {
    color: #666666;
    margin-right: 30rpx;
  }
  .circular {
    margin-right: 10rpx;
  }
}
.circular {
  width: 34rpx;
  height: 34rpx;
  border-radius: 50%;
}
.white {
  background: #ffffff;
  border: 1px solid #bbbbbb;
  color: #999999;
}
.green {
  background: #ebf2f5;
  border: 1px solid #59c416;
  color: #0cc437;
}
.red {
  color: #a14430;
  background: #efdedb;
  border: 1px solid #a14430;
}
.main {
  overflow: auto;
  height: calc(100vh - 400rpx);
  padding: 30rpx 10rpx 40rpx;

  .circular {
    width: 90rpx;
    height: 90rpx;
    margin-left: 30rpx;
    margin-top: 30rpx;
    text-align: center;
    line-height: 90rpx;
  }
  .circular-box {
    display: flex;
    flex-wrap: wrap;
    margin-bottom: 50rpx;
  }
}
.footer {
  position: fixed;
  bottom: 40rpx;
  left: 50%;
  transform: translateX(-50%);
  .submit {
    text-align: center;
    width: 600rpx;
    height: 98rpx;
    line-height: 98rpx;
    font-size: 36rpx;
    color: #ffffff;
    background: #454a5e;
    border-radius: 50rpx 50rpx 50rpx 50rpx;
  }
}
</style>